<template>
  <!-- 搜索 -->
  <ContentWrap>
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <!-- <el-form-item label="资产类型" prop="status">
        <el-select v-model="queryParams.status" class="!w-280px" clearable placeholder="全部">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.METON_CARD_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="资产状态" prop="status">
        <el-select v-model="queryParams.status" class="!w-280px" clearable placeholder="全部">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.METON_CARD_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table lazy v-loading="loading" :data="list" row-key="id" ref="tableRef">
      <el-table-column label="ID" align="center" prop="id" width="120" />
      <el-table-column label="资产名称" align="left" prop="spuName" min-width="250">
        <template #default="{ row }">
          <div class="flex">
            <div class="flex items-center">
              <div class="mr-[10px] flex items-center">
                <el-image :src="row.picUrl" class="!h-[48px] !w-[48px] border-rd-4px" />
              </div>

              <ElTooltip :content="row.spuName" placement="top">
                <span class="overflow-ellipsis overflow-hidden">
                  {{ row.spuName }}
                </span>
              </ElTooltip>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="属性" align="center" prop="skuType">
        <template #default="scope">
          <DictText :type="DICT_TYPE.PRODUCT_SPU_TYPE4" :value="scope.row.skuType" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="余额" min-width="100" prop="leavePrice">
        <template #default="scope">
          <span>{{ floatToFixed2(scope.row.balance) + '元' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="普通余额" min-width="100" prop="leavePrice">
        <template #default="scope">
          <span>{{ floatToFixed2(scope.row.masterBalance) + '元' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="赠送余额" min-width="100" prop="leavePrice">
        <template #default="scope">
          <span>{{ floatToFixed2(scope.row.giveBalance) + '元' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="有效期" align="center" prop="expireDate" :formatter="dateFormatter" />
      <el-table-column label="来源" align="center" min-width="70" prop="source">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.TENANT_IDENTITY" :value="row.source" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.METON_CARD_STATUS" :value="row.status" />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as PayApi from '@/api/shop/trade/pay'
import { floatToFixed2 } from '@/utils'

import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'

const { userId } = defineProps({
  userId: {
    type: Number,
    required: true
  }
})
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: userId,
  status: undefined // 卡项状态
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PayApi.getAssetsList(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const tableRef = ref()
// 点击其他信息
const expandMsg = (row?: any) => {
  // expand 展开
  console.log(tableRef.value)
  row.isShowMsg = !row.isShowMsg
  tableRef.value.toggleRowExpansion(row)
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const emits = defineEmits(['updateBalance'])

/** 初始化 **/
onMounted(() => {
  queryParams.userId = userId
  getList()
})
</script>
